package com.atguigu.tingshu.search.config;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolExecutorConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        return new ThreadPoolExecutor(
                3,
                5,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    @Bean
    public ThreadPoolExecutor threadPoolExecutor1(){
        //  自定义线程池-不能使用工具类创建线程池. 阻塞队列或最大线程个数是21亿，这样会导致OOM!
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                8,    //  核心线程数 IO:2n  cpu:n+1
                16,   //  最大线程数
                3,  //  空闲线程存活时间
                TimeUnit.SECONDS, // 空闲线程存活时间单位
                new ArrayBlockingQueue<>(10) // 阻塞队列
        );
        return threadPoolExecutor;
    }

}
